package com.boarsoft.boar.soagov.app.biz;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.entity.AppLib;
import com.boarsoft.boar.entity.DirInfo;
import com.boarsoft.boar.soagov.app.AppLibFaceBiz;
import com.boarsoft.boar.soagov.entity.FaceInfo;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("appLibFaceBiz")
public class AppLibFaceBizImpl extends SimpleBizImpl implements AppLibFaceBiz {

	@Override
	@Transactional(readOnly = true)
	public PagedResult<FaceInfo> list(String appId, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from ").append(FaceInfo.class.getName())//
			.append(" f where f.id in (select d.id from ").append(DirInfo.class.getName())//
			.append(" d where d.parentId in (select al.libId from ").append(AppLib.class.getName())//
			.append(" al where al.appId = '").append(appId).append("'))");
		if (Util.strIsNotEmpty(key)) {
			sb.append("and (f.code like '%").append(key);
			sb.append("%' or f.name like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal("select count(f.id)".concat(sb.toString()));
		if (Util.strIsNotEmpty(orderBy)) {
			sb.append(" order by f.").append(orderBy);
		} else {
			sb.append(" order by f.name");
		}
		List<FaceInfo> lt = dao.list("select f".concat(sb.toString()), pageNo, pageSize);
		return new PagedResult<FaceInfo>(total, lt, pageNo, pageSize);
	}

}